package com.interview.controller;

import com.interview.feign.FeignInterviewService;
import com.interview.utils.JsonTurnUtils;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/feign_solr")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class FeignSolrController {

    @Autowired
    private FeignInterviewService feignSolrService;

    //http://localhost:8083/feign_solr?title=怎么

    @GetMapping
    @HystrixCommand
    public String soleHighlighting(@RequestParam("title") String title){
        String s = null;
        try {
            List<Map<String, String>> list = feignSolrService.soleHighlighting(title);
            s = JsonTurnUtils.objTurnJson( list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  s;

    }

    //降级方法
    public String payment_Global_FallbackMethod() {
        String str = "<div style=\"width: 100%;text-align: center\">\n" +
                "    <h1 >对不起，网络太拥挤了！ 刷新重试-----Feign</h1>\n" +
                "</div>";
        return str;
    }



    //http://localhost:8083/feign_article/hystrix
    //测试 熔断是否触发
    @GetMapping("/hystrix")
    @ResponseBody
    @HystrixCommand
    public String testHystrix() throws InterruptedException {
        //这个触发了 feign-service 内的 超时处理
       Thread.sleep(5000);

       return  "超时";
    }

}
